package com.novellotus.iov.carvice.utils;

import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.util.DisplayMetrics;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.novellotus.iov.carvice.R;
import com.novellotus.iov.carvice.retrofit.RetrofitFactory;

/**
 * Created by Rich on 2017/7/6.
 */

public class MyViewUtils {

    /**
     * 获取状态栏高度
     */
    public static int getStatusBarHeight(Context context) {
        Resources resources = context.getResources();
        int resourceId = resources.getIdentifier("status_bar_height", "dimen", "android");
        int height = resources.getDimensionPixelSize(resourceId);
        return height;
    }

    /**
     * 设置按钮是否可以点击
     */
    public static void setButtonClickable(TextView button, Boolean click) {
        if (click) {
            button.setClickable(true);
            button.setTextColor(Color.parseColor("#ffffff"));
            button.setBackgroundResource(R.drawable.selector_btn_click);
        } else {
            button.setClickable(false);
            button.setTextColor(Color.parseColor("#cccccc"));
            button.setBackgroundResource(R.drawable.selector_btn_click);
        }
    }

    /**
     * @param context
     * @return 获取LinearLayoutManager
     * 单列
     */
    public static LinearLayoutManager getLinearLayoutManager(Context context) {
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        return linearLayoutManager;
    }

    /**
     * @param context
     * @param count
     * @return 多列
     */
    public static GridLayoutManager gridLayoutManager(Context context, int count) {
        GridLayoutManager linearLayoutManager = new GridLayoutManager(context, count);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        return linearLayoutManager;
    }

    /**
     * @param count
     * @return 多行瀑布流
     */
    public static StaggeredGridLayoutManager getStaggeredGridLayoutManager(int count) {
        StaggeredGridLayoutManager staggeredGridLayoutManager;
        staggeredGridLayoutManager = new StaggeredGridLayoutManager(count, StaggeredGridLayoutManager.VERTICAL);
        return staggeredGridLayoutManager;
    }

    /**
     * @param url
     * @param imageView
     * @param context   加载图片
     */
    public static void initImage(String url, ImageView imageView, Context context) {
        String http = RetrofitFactory.imgUrl;
        RequestOptions options = new RequestOptions()
                .placeholder(R.drawable.icon_error).error(R.drawable.icon_error).skipMemoryCache(true).diskCacheStrategy(DiskCacheStrategy.RESOURCE).dontAnimate();
        Glide.with(context).load(http + url).apply(options).into(imageView);
    }


    /**
     * 设置popupwindow外面背景透明度
     * @param alpha 透明度  0-1   0-透明   1-不透明
     */
    public static void backgroundAlpha(Activity activity, float alpha) {
        WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
        lp.alpha = alpha;
        activity.getWindow().setAttributes(lp);
    }


    /**
     * @param manager
     * @return 获取屏幕高度
     */
    public static int getSystemHeight(WindowManager manager) {
        int height;
        DisplayMetrics outMetrics = new DisplayMetrics();
        manager.getDefaultDisplay().getMetrics(outMetrics);
        // int width = outMetrics.widthPixels;
        height = outMetrics.heightPixels;
        return height;
    }

    /**
     * @param manager
     * @return 获取屏幕高度
     */
    public static int getSystemWidth(WindowManager manager) {
        int width;
        DisplayMetrics outMetrics = new DisplayMetrics();
        manager.getDefaultDisplay().getMetrics(outMetrics);
        width = outMetrics.widthPixels;
        return width;
    }

    /**
     * 根据手机分辨率从DP转成PX
     *
     * @param context
     * @param dpValue
     * @return
     */
    public static int dipTopx(Context context, float dpValue) {
        float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    public static Bitmap drawableToBitmap(Resources res, int id) {
        Bitmap bmp = BitmapFactory.decodeResource(res, id);
        return bmp;
    }
}
